package com.xujc.dao.imp;

import com.xujc.dao.IFormDAO;
import com.xujc.entity.FormEntity;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xujc on 6/17/15.
 */
public class FormDAO extends HibernateDaoSupport implements IFormDAO{

    @Override
    public int createForm(String formName, String formDescribe) {
        Session session = getSessionFactory().openSession();
        FormEntity entity=new FormEntity();
        entity.setName(formName);
        entity.setFormDescribe(formDescribe);
        Transaction transaction = session.beginTransaction();
        session.save(entity);
        transaction.commit();
        session.close();
        return entity.getFId();
    }

    @Override
    public List<FormEntity> getAllForm() {
        Session session = getSessionFactory().openSession();
        Query query = session.createQuery("from FormEntity ");
        List list = query.list();
        List<FormEntity> formEntitles=new ArrayList<FormEntity>();
        formEntitles.addAll(list);
        session.close();
        return formEntitles;
    }

    @Override
    public FormEntity getFormById(int fid) {
        Session session = getSessionFactory().openSession();
        Query query = session.createQuery("from FormEntity where FId=?");
        query.setParameter(0,fid);
        Object o = query.list().get(0);
        session.close();
        return ((FormEntity) o);

    }
}
